package com.adapters;

import java.util.List;

import com.objects.VaultFile;
import com.senditcertified.mail.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class FileAttachmentsAdapter extends ArrayAdapter<VaultFile> {
	private OnClickAction mAction;

	public FileAttachmentsAdapter(Context context, List<VaultFile> objects) {
		super(context, R.layout.attach_file_item, objects);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(getContext()).inflate(
					R.layout.attach_file_item, parent, false);
			holder = new ViewHolder();
			holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tvName.setSelected(true);
			holder.btnView = convertView.findViewById(R.id.btn_view_file);
			holder.btnRemove = convertView.findViewById(R.id.btn_remove_file);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvName.setText(getItem(position).getName());
		holder.btnView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mAction != null) {
					mAction.viewFile(getItem(position), position);
				}
			}
		});
		holder.btnRemove.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mAction != null) {
					mAction.removeFile(getItem(position), position);
				}
			}
		});
		return convertView;
	}

	public void setAction(OnClickAction action) {
		mAction = action;
	}

	private class ViewHolder {
		TextView tvName;
		View btnView, btnRemove;
	}

	public interface OnClickAction {
		public void viewFile(VaultFile file, int position);

		public void removeFile(VaultFile file, int position);
	}
}
